'use strict';
const db = uniCloud.database();
// 关联 Accounts 集合
const accountsCollection = db.collection('Accounts');

exports.main = async (event, context) => {
  // 获取前端传递的待删除用户 ID
  const { _id } = event;

  // 1. 校验参数
  if (!_id) {
    return {
      code: -1,
      msg: "删除失败：用户 ID 不能为空"
    };
  }

  try {
    // 2. 根据 ID 删除数据
    const deleteResult = await accountsCollection.doc(_id).remove();

    // 3. 判断删除结果
    if (deleteResult.deleted === 1) {
      return {
        code: 0,
        msg: "删除成功",
        data: deleteResult
      };
    } else {
      return {
        code: -2,
        msg: "删除失败：未找到该用户数据",
        data: deleteResult
      };
    }
  } catch (err) {
    // 4. 捕获数据库异常
    console.error("删除操作异常：", err);
    return {
      code: -3,
      msg: "删除失败：" + err.message,
      data: null
    };
  }
};